package com.jboost.modules.app.service.strategry.gc;

import com.google.common.collect.Maps;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.app.dto.resp.GcGuiGeResp;
import com.jboost.modules.app.dto.resp.GcProductResp;
import com.jboost.modules.app.dto.resp.GcWlResp;
import com.jboost.modules.system.entity.AJdySyncData;
import com.jboost.modules.system.mapper.GcDataMapper;
import com.jboost.modules.system.mapper.HsMaterialNeedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static com.jboost.modules.app.service.strategry.gc.HsGcKcQuery.PRODUCT_INFO;

@Service
@Slf4j
public abstract class AbstractGcService<T, V> implements Strategy<T,V> {
    @Autowired
    GcDataMapper gcDataMapper;
    public final static String url_prefix_test = "https://oms.goodcang.net/public_open";
    private final static String WL_API = url_prefix_test + "/base_data/get_shipping_method";

    private final static String GUIGE = url_prefix_test + "/product/get_category";

    public final static String PRODUCT_INFO = url_prefix_test+"/product/get_product_sku_list";
   public HttpHeaders getJdyHeaders(){
       HttpHeaders jdyHeaders = new HttpHeaders();
       jdyHeaders.set("Authorization","Bearer "+ "bdfDElitwqhraWZoCv3uxdv9PysCV605");
       return jdyHeaders;
    }

    public String getAppId() {
        return "64880dc1f3ecc50008fbae61";
    }
    protected HttpHeaders gcHeaders;
    {
        gcHeaders = new HttpHeaders();
        gcHeaders.add("app-token","e05fb0f81100dd82cfe02d40c8b9ba7e");
        gcHeaders.add("app-key","9625ba40533676fc00d9773273ae778e");

    }


    public boolean pass(String id,Object object) throws IOException {
        AJdySyncData aJdySyncData = gcDataMapper.selectById(id);
        if (Objects.isNull(aJdySyncData)){
            return true;
        }

        jdyDelete(aJdySyncData.getDataType());
        gcDataMapper.deleteById(aJdySyncData);
        return true;
    }


    public void save(String id,String data,String dataType){
        AJdySyncData aJdySyncData = new AJdySyncData();
        aJdySyncData.setId(id);
        aJdySyncData.setData(data);
        aJdySyncData.setDataType(dataType);
        gcDataMapper.insert(aJdySyncData);
    }

    public GcProductResp.DataBean findBySku(String sku){
        List<GcProductResp.DataBean> data = null;
        //获取商品信息
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("product_sku", sku);
        try {
            String post = HttpRestUtils.post(PRODUCT_INFO, param, getJdyHeaders());
            GcProductResp gcProductResp = JSONUtil.fromJson(post, GcProductResp.class);
            data = gcProductResp.getData();
        } catch (IOException ex) {
            log.warn("获取商品信息错误");
        }
        return data.get(0);
    }


    public GcWlResp.DataBean findByWarehouseCode(String warehouseCode){
        List<GcWlResp.DataBean> data = null;
        //获取商品信息
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("warehouseCode", warehouseCode);
        try {
            String post = HttpRestUtils.post(WL_API, param, getJdyHeaders());
            GcWlResp gcProductResp = JSONUtil.fromJson(post, GcWlResp.class);
            data = gcProductResp.getData();
        } catch (IOException ex) {
            log.warn("获取商品信息错误");
        }
        return data.get(0);
    }

    public GcGuiGeResp findGuiges(){
        GcGuiGeResp data = null;
        try {
            String post = HttpRestUtils.post(WL_API, Maps.newHashMap(), getJdyHeaders());
            data = JSONUtil.fromJson(post, GcGuiGeResp.class);
        } catch (IOException ex) {
            log.warn("获取商品规格信息错误");
        }
        return data;
    }

    public void save(AJdySyncData aJdySyncData){
        gcDataMapper.insert(aJdySyncData);
    }

}
